<?php

class Admini_SystemController extends Uuch_ManageController{
    
    public function init() {
        parent::init();        
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->view->type="system";
    }    
       
    public function indexAction(){
        
       
    }
    
    public function optionitemAction(){
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $pagesize=14;   
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $name = $this->_getParam("name");
        $res = Application_Model_OptionItem::getFilter($name,$pagesize,$pageindex);
        $this->view->name = $name;
        $this->view->optionlist = $res["list"];
        $this->view->optionall = $res;
        $this->view->stype = 17;       
    }   
    
    public function editAction(){
        $id = $this->_getParam("id");
        $optionItem = Application_Model_OptionItem::load($id);
        if($optionItem){
            $this->json(array('type'=>1,'mykey'=>$optionItem->mykey,'myvalue'=>$optionItem->myvalue,'description'=>$optionItem->description));
        }else{
            $this->json(array('type'=>2));
        }
    }    
    	
    public function addprocessAction(){
        $id = $this->_getParam('optionid');
        $mykey = $this->_getParam('mykey');
        $myvalue = $this->_getParam('myvalue');
        $description = $this->_getParam('description');
        $opt = new Application_Model_OptionItem();
        $opt->id = $id;
        $opt->mykey = $mykey;
        $opt->myvalue = $myvalue;
        $opt->description = $description;
        $opt->save();
        $this->redirect('optionitem', 'system');
    }
    
    public function statuslistAction(){
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $pagesize = 14;
        $pageindex = $this->_getParam('start');
        $name = $this->_getParam('name');
        if(!$pageindex){
            $pageindex = 0;
        }
        $res = Application_Model_Status::getFilter($name, $pagesize, $pageindex);
        $this->view->name = $name;
        $this->view->statuslist = $res['list'];
        $this->view->statusall = $res;
        $this->view->stype = 18;
    }
    
    public function statusprocessAction(){
        $id = $this->_getParam('statusid');
        $name = $this->_getParam('name');
        $statusValue = $this->_getParam('statusValue');
        $statusType = $this->_getParam('statusType');
        $remark = $this->_getParam('remark');
        $opt = new Application_Model_Status();
        $opt->id = $id;
        $opt->name = $name;
        $opt->statusValue = $statusValue;
        $opt->statusType = $statusType;
        $opt->remark = $remark;
        $opt->save();
        $this->redirect('statuslist', 'system');
    }
    
    public function editstatusAction(){
        $id = $this->_getParam("id");
        $status = Application_Model_Status::load($id);
        if($status){
            $this->json(array('type'=>1,'name'=>$status->name,'statusValue'=>$status->statusValue,'statusType'=>$status->statusType,'remark'=>$status->remark));
        }else{
            $this->json(array('type'=>2));
        }
    }
    
    /**
     * 推广链接列表
     */
    public function adlinkAction(){
        $this->setTitle("推广链接管理");
        $pagesize = 15;   
        $pageindex = $this->_getParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->view->result = Application_Model_AdLink::getByFilter($pagesize,$pageindex);
        $this->view->list = $this->view->result['list'];
        $this->view->stype = 23;
    }
    
    public function linkdetailAction(){
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->setTitle("推广链接详情");
        $id = $this->getSafeParam("id");
        $this->view->detail = Application_Model_AdLink::load($id);
    }
  
    /**
     * 添加推广链接
     */
    public function increaselinkAction(){     
        error_reporting(NULL);
        ini_set('display_errors','Off');
        $alid = $this->getSafeParam("alid");
        if($alid){
            $item = Application_Model_AdLink::load($alid);
        }else{
            $item = new Application_Model_AdLink();
        }        
        $item->Name = $this->getSafeParam("name");
        $item->Target = $this->getSafeParam("target");
        $item->Source = $this->getSafeParam("source");
        $item->Media = $this->getSafeParam("media");
        $item->Campaign = $this->getSafeParam("campaign");
        $item->Description = $this->getSafeParam("description");
        $item->Code = $this->getSafeParam("code");
        $item->getMyTitle($this->getSafeParam("title"));
        $item->getMyContent($this->getSafeParam("content"));
        $item->save();
        if($alid){
            $this->json(2);
        }else{
            $this->json(1);
        }
    }
    
    /**
     * 根据ID获取一条推广链接记录
     */
    public function getadlinkAction(){
        error_reporting(NULL);
        ini_set('display_errors','Off');
        $id = $this->getSafeParam("id");
        $item = Application_Model_AdLink::load($id);
        $this->json(array('name'=>$item->name,'target'=>$item->target,'source'=>$item->source,'media'=>$item->media,
            'campaign'=>$item->campaign,'code'=>$item->code,'content'=>$item->FormatContent,'title'=>$item->FormatTitle,'description'=>$item->Description,'alid'=>$item->Id));
    }
    
    /**
     * 根据ID 删除一条推广链接记录
     */
    public function deladlinkAction(){
        $id = $this->getSafeParam("id");
        $item = Application_Model_AdLink::load($id);
        $item->delete();
        $this->json(1);
    }
    
    /**
     * 代购推广链接
     */
    public function purchaseAction(){
        $this->setTitle("代购推广");        
        $this->view->stype = 26;        
    }
    
    public function purchaseiframeAction(){
        $this->disableLayout();
    }
    
    
    
    public function testAction(){
        $item = new Uuch_Track_SingpostAPI("http://www.singpost.com/index.php?option=com_tablink&controller=tracking&task=trackdetail&layout=show_detail&tmpl=component&ranumber=RQ203195536SG");
        $result = $item->query();
        var_dump($result);
        exit();
    }

    /**
     * 设置系统汇率
    */
    public function rateAction()
    {
        $rate = Application_Model_SystemConfig::getRateByName('rate');
        if($this->getRequest()->isPost()){
            $value  = $this->getSafeParam('rate');

            $re = array(
                    's'     => -1,
                    'm'     => '',
                    'd'     => null,
                );

            if(is_numeric($value)){
                $data = array(
                            'id'    => $rate->id,
                            'value' => $value,
                        );
                Application_Model_SystemConfig::dataUp($data);
                $re = array(
                    's'     => 0,
                    'm'     => '更新成功',
                    'd'     => null,
                );
            }else{
                $re = array(
                    's'     => 41,
                    'm'     => '参数异常',
                    'd'     => null,
                );
            }
            echo json_encode($re);
            exit;
        }else{
            $this->view->rate = $rate;
        }
    }

    /**
     *  短信列表
    */
    public function smsAction()
    {
        $this->setTitle("短信发送历史");
        $pagesize = 20;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $phone = $this->getSafeParam("phone");
        $ptype  = $this->getSafeParam("type");
        $smslist = Application_Model_Sms::getList($phone, $ptype, $pagesize, $pageindex);
        $this->view->smslist    = $smslist["list"];
        $this->view->smslistAll = $smslist;
        $this->view->phone      = $phone;
        $this->view->ptype      = $ptype;
        $this->view->type = "system";
    }

    /**
     * 查询短息余额
    */
    public function smsbalanceAction()
    {
        // 
        $re = array('s' => -1);
        try {
            $balance = Application_Model_Sms::balance();
            if($balance == false){
                $re = array('s' => -2);
            }else{
                $re = array('s' => $balance);
            }
        } catch (Exception $e) {
            $re = array('s' => -2);
        }
        echo json_encode($re);
        exit;
    }
}